package face.pay.web.user.controller;

import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.util.ResultMessage;
import face.pay.web.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: zkj
 * @Date: 2019/9/4 11:58
 * @Description:
 */
@Controller
@RequestMapping("/safe/")
@Slf4j
public class UsertSafeController {


    @Autowired
    IUserService iUserService;


    @RequestMapping(value = "/change_pwd", method = RequestMethod.GET)
    public String changePwdPage(HttpServletRequest httpServletRequest) {


        return "user/changePwd";
    }

    /**
     * 用户手机信息
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/mobile_info")
    public String mobileInfo(@CurrentUser UserInfo loginUser, HttpServletRequest httpServletRequest,Model model) {
        loginUser.setDeleted(0);
        UserInfo userInfo=iUserService.queryUserInfo(loginUser);

        model.addAttribute("mobile",userInfo.getMobile());
        model.addAttribute("userId",userInfo.getId());

        return "user/mobileInfo";
    }


    @RequestMapping(value = "/modify_pwd", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage passWordModify(String validateCode, String newPassword, @CurrentUser UserInfo loginUser, HttpServletRequest httpServletRequest) {

        return  iUserService.modifyPassword(loginUser, validateCode,  newPassword);

    }


    /**
     * 用户资料查询
     * @param loginUser
     * @param model
     * @return
     */
    @RequestMapping(value = "/userInfo", method = RequestMethod.GET)
    public String myData(@CurrentUser UserInfo loginUser, Model model) {
        //判断当前用户是否登录
        if(null==loginUser){
            return "login";
        }
        loginUser.setDeleted(0);
        UserInfo userdata=iUserService.queryUserInfo(loginUser);

        model.addAttribute("userInfo",userdata);
        return "user/userInfo";
    }

    /**
     * 更新用户信息
     * @param userInfo
     * @return
     */
    @PostMapping("/updateUserInfo")
    @ResponseBody
    public ResultMessage updateUserInfo(UserInfo userInfo, String code , @CurrentUser UserInfo loginUser){
        //获取当前用户的id
        if(!loginUser.getId().equals(userInfo.getId())){
            //前端id和用户id不一样,被人串改不能修改信息
            log.info("用户登录信息被劫持:"+loginUser.getUserName());
            return ResultMessage.createFail("修改失败");
        }
        userInfo.setUpdatedBy(loginUser.getId());
        return iUserService.updateUserInfo(userInfo,code,loginUser.getId());

    }


    @PostMapping("/updateMobile")
    @ResponseBody
    public ResultMessage updateMobile(UserInfo userInfo, String validateCode , @CurrentUser UserInfo loginUser){
        //获取当前用户的id
        if(!loginUser.getId().equals(userInfo.getId())){
            //前端id和用户id不一样,被人串改不能修改信息
            log.info("用户登录信息被劫持:"+loginUser.getUserName());
            return ResultMessage.createFail("修改失败");
        }
        userInfo.setUpdatedBy(loginUser.getId());
        userInfo.setDeleted(0);
        return iUserService.updateMobile(userInfo,validateCode,loginUser);

    }


    @RequestMapping(value = "/sendValidateCode")
    @ResponseBody
    public ResultMessage sendValidateCode(@CurrentUser UserInfo loginUser,String bizType) {
        return  iUserService.sendValidateCode(loginUser,bizType);
    }

}
